package com.visionblue.business.ship.page.dialog

import android.app.Activity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.visionblue.business.ship.R
import com.visionblue.business.ship.model.repository.db.RecordAndOrderRelation
import com.visionblue.business.ship.util.ext.setVisible

/**
 * @author: xlh
 * @date: 2020-06-05 3:12 PM
 * @desc:
 */
class CachedOrderDialog(activity: Activity, private val orders: List<RecordAndOrderRelation>?) {

    private var page: Int = 1
    private val size = 2
    private var maxPage: Int = 0

    private val mDialog: BlurrySlideDialog = BlurrySlideDialog(
        activity,
        R.layout.layout_cached_orders
    )

    private val mAdapter = CachedOrderAdapter()

    init {
        val rvOrders = mDialog.findViewById<RecyclerView>(R.id.rvOrders)
        rvOrders.layoutManager = LinearLayoutManager(mDialog.context)
        rvOrders.adapter = mAdapter

        mDialog.findViewById<ImageView>(R.id.ivClose).setOnClickListener {
            mDialog.dismiss()
        }

        if (orders.isNullOrEmpty()) {
            mDialog.findViewById<View>(R.id.viewEmpty).visibility = View.VISIBLE
            rvOrders.visibility = View.INVISIBLE
            mDialog.findViewById<TextView>(R.id.tvPre).visibility = View.INVISIBLE
            mDialog.findViewById<TextView>(R.id.tvNext).visibility = View.INVISIBLE
        } else {
            rvOrders.visibility = View.VISIBLE
            mDialog.findViewById<TextView>(R.id.tvPre).visibility = View.VISIBLE
            mDialog.findViewById<TextView>(R.id.tvNext).visibility = View.VISIBLE
            mDialog.findViewById<View>(R.id.viewEmpty).visibility = View.GONE

            maxPage = (orders.size + 1) / size
            mAdapter.onItemClickListener = object : CachedOrderAdapter.OnItemClickListener {
                override fun onItemClick(position: Int) {
                    mDialog.dismiss()
                }
            }

            mDialog.findViewById<TextView>(R.id.tvPre).setOnClickListener {
                page--
                updateView()
            }

            mDialog.findViewById<TextView>(R.id.tvNext).setOnClickListener {
                page++
                updateView()
            }

            updateView()
        }


    }

    private fun updateView() {
        mAdapter.setData(subOrders((page - 1) * size, page * size))
        setPreNext()
    }

    private fun setPreNext() {
        mDialog.findViewById<TextView>(R.id.tvPre).setVisible(page > 1)
        mDialog.findViewById<TextView>(R.id.tvNext).setVisible(page < maxPage)
    }

    private fun subOrders(from: Int, to: Int): List<RecordAndOrderRelation> {
        val orders = this.orders ?: return mutableListOf()
        val fromIndex = if (from < 0) 0 else from
        val toIndex = if (to > orders.size) orders.size else to
        return orders.subList(fromIndex, toIndex)
    }

    fun show(
//        onViewsClickListener: OnViewsClickListener? = null
    ) {
        mDialog.show()
    }

//    fun update() {
//        mAdapter.setData(orders)
//    }

//    interface OnViewsClickListener {
//        /**
//         * 上一个
//         */
//        fun onPreClick()
//
//        /**
//         * 下一个
//         */
//        fun onNextClick()
//    }
}